﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Utils.ControlToLoad;
using Utils.Exceptions;
using Core;
using Core.DAL;

namespace xManagerWebApp.EditControls
{
    public partial class ControlTemplateDislocations : System.Web.UI.UserControl, IControlToLoadRW
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //bool requiresDepart = false;
            //bool requiresArr = false;

            //using (DataAccessDataContext dc = new DataAccessDataContext())
            //{
            //    var queryRequiresDepart = from exp in dc.ExpenseTypeDislocations
            //                              where exp.Id.Equals(this.IdR)
            //                              select exp.RequiresDeparture;
            //    requiresDepart = queryRequiresDepart.Single<bool>();
            //    var queryRequiresArr = from exp in dc.ExpenseTypeDislocations
            //                           where exp.Id.Equals(this.IdR)
            //                           select exp.RequiresArrival;
            //    requiresArr = queryRequiresArr.Single<bool>();
            //}
            //if (requiresDepart)
            //{
            //    this.lb_Departure.Visible = true;
            //    this.tb_Departure.Visible = true;
            //}
            //if (requiresArr)
            //{
            //    this.lb_Arrival.Visible = true;
            //    this.tb_Arrival.Visible = true;
            //}
        }

        public bool ReadOnly
        {
            get
            {
                if (ViewState["ReadOnly"] != null)
                    return (bool)ViewState["ReadOnly"];
                else
                    return false;
            }
            set { ViewState["ReadOnly"] = value; }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            bool requiresDepart = false;
            bool requiresArr = false;

            using (DataAccessDataContext dc = new DataAccessDataContext())
            {
                var queryRequiresDepart = from exp in dc.ExpenseTypeDislocations
                                          where exp.Id.Equals(this.TypeId)
                                          select exp.RequiresDeparture;
                requiresDepart = queryRequiresDepart.Single<bool>();
                var queryRequiresArr = from exp in dc.ExpenseTypeDislocations
                                       where exp.Id.Equals(this.TypeId)
                                       select exp.RequiresArrival;
                requiresArr = queryRequiresArr.Single<bool>();
            }
            if (requiresDepart)
            {
                this.lb_Departure.Visible = true;
                this.tb_Departure.Visible = true;
            }
            else
            {
                this.lb_Departure.Visible = false;
                this.tb_Departure.Visible = false;
            }
            if (requiresArr)
            {
                this.lb_Arrival.Visible = true;
                this.tb_Arrival.Visible = true;
            }
            else
            {
                this.lb_Arrival.Visible = false;
                this.tb_Arrival.Visible = false;
            }

            if (this.ReadOnly)
                this.SetFieldsReadOnly();
        }

        private void SetFieldsReadOnly()
        {
            this.tb_Arrival.Enabled = false;
            this.tb_Departure.Enabled = false;
            this.tb_Distance.Enabled = false;
        }

        #region IControlToLoad Members

        public Guid? Id
        {
            get
            {
                if (ViewState["Id"] != null)
                    return (Guid?)ViewState["Id"];
                else
                    return null;
            }
            set { ViewState["Id"] = value; }
        }

        public void Save()
        {
            ExpenseLineDislocation exp = new ExpenseLineDislocation();
            exp.Id = this.Id.Value;
            exp.Distance = Convert.ToInt32(this.tb_Distance.Text);
            if (this.tb_Arrival.Visible)
                exp.Arrival = this.tb_Arrival.Text;
            if (this.tb_Departure.Visible)
                exp.Departure = this.tb_Departure.Text;
            using (DataAccessLayer dal = new DataAccessLayer())
            {
                exp.Save(dal);
            }
            //using (DataAccessDataContext dc = new DataAccessDataContext())
            //{
            //    var query = from e in dc.ExpenseLines
            //                where e.Id.Equals(this.Id)
            //                select e;
            //    ExpenseLine expLine = query.Single<ExpenseLine>();
            //    expLine.Value = exp.GetExpenseValue();
            //    dc.ExpenseLineDislocations.InsertOnSubmit(exp);
            //    dc.SubmitChanges();
            //}
        }
        #endregion

        #region IControlToLoadRW Members

        public Guid? TypeId
        {
            get
            {
                if (ViewState["TypeId"] != null)
                    return (Guid?)ViewState["TypeId"];
                else
                    return null;
            }
            set { ViewState["TypeId"] = value; }
        }
        
        public void Bind(Guid Id, Guid TypeId)
        {
            this.Id = Id;
            this.TypeId = TypeId;
        }

        public void BindControls(Guid id)
        {
            this.Id = id;
            using (DataAccessLayer dal = new DataAccessLayer())
            {
                ExpenseLineDislocation e = ExpenseLineDislocation.Get(dal, this.Id.Value);
                if (e != null)
                    this.LoadFields(e);
            }
        }

        public void BindType(Guid TypeId)
        {
            this.TypeId = TypeId;
        }

        public void ClearFields()
        {
            this.tb_Arrival.Text = string.Empty;
            this.tb_Departure.Text = string.Empty;
            this.tb_Distance.Text = string.Empty;
        }

        #endregion

        private void LoadFields(ExpenseLineDislocation e)
        {
            this.tb_Distance.Text = Convert.ToString(e.Distance);
            if (!String.IsNullOrEmpty(e.Arrival))
                this.tb_Arrival.Text = e.Arrival;
            if (!String.IsNullOrEmpty(e.Departure))
                this.tb_Departure.Text = e.Departure;
        }
    }
}